<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/9
 * Time: 下午4:23
 */

namespace app\api\model;


use think\Db;
use think\Request;

class Base extends \app\common\model\Base
{
    /**
     * 根据有Id修改信息 无Id 新增信息
     * @param $data
     * @return false|int|string
     * @throws
     */
    public function editData($data){
        if (isset($data['id'])){
            if (is_numeric($data['id']) && $data['id']>0){
                $save = $this->allowField(true)->save($data,[ 'id' => $data['id']]);
            }else{
                $save  = $this->allowField(true)->save($data);
            }
        }else{
            $save  = $this->allowField(true)->save($data);
        }
        if ( $save == 0 || $save == false) {
            $res=false;
        }else{
            $res=true;
        }
        return $res;
    }

    /**
     * 保存修改信息
     * @param $original_data
     * @param $save_data
     * @return bool
     */
    protected static function saveEditLog($guid,$original_data,$save_data){
        if (empty($original_data)&&empty($save_data)){
            return false;
        }
        $log_data=[
            "guid"=>$guid,
            "model_data"=>get_called_class(),
            "original_data"=>json_encode($original_data),
            "save_data"=>json_encode($save_data),
            "create_time" => date("Y-m-d H:i:s"),
            "update_time" => date("Y-m-d H:i:s"),
            "controller" => Request::instance()->controller(),
            "action" => Request::instance()->action()
        ];
        Db::table("t_update_log")->insert($log_data);
        return true;
    }

    protected static function saveCreateLog($guid,$save_data){
        if (empty($save_data)){
            return false;
        }
        $log_data=[
            "guid"=>$guid,
            "model_data"=>get_called_class(),
            "save_data"=>json_encode($save_data),
            "create_time"=>date('Y-m-d H:i:s'),
            "update_time"=>date('Y-m-d H:i:s'),
            "controller" => Request::instance()->controller(),
            "action" => Request::instance()->action()
        ];
        Db::table("t_create_log")->insert($log_data);
        return true;
    }

    public static function addInfo($guid,$data)
    {
        $row = self::create($data);
        self::saveCreateLog($guid,$data);
        return $row;
    }

    public static function getInfo($where,$field=true)
    {
        return self::get(function($query)use($where,$field){
            return $query->field($field)->where($where);
        });
    }

    public static function updateInfo($guid,$odata,$data,$where)
    {
        $row = self::update($data,$where);
        self::saveEditLog($guid,$odata,$data);
        return $row;
    }

    public static function getInfoList($where,$field,$page=null,$with=null)
    {
        return self::all(function($query)use($where,$page,$field,$with){
            if (!empty($page)) {
                $query->page($page,config('list_rows_20'));
            }
            if (!empty($where)) {
                $query->where($where);
            }
            if (!empty($field)) {
                $query->field($field);
            }
            if (!empty($with)) {
                $query->with($with);
            }
            return $query;
        });
    }
}